<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="isEdit ? '修改' : '新增'"
    width="800px"
    :visible.sync="visible"
  >
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      :label-position="labelPosition"
      label-width="150px"
    >
      <el-row :gutter="24">
        <el-col>
          <el-form-item label="时间">
            <el-date-picker
              type="date"
              :style="inputWidth"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.evalDate"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="民主测评好的比例" prop="goodPct">
            <el-input v-model.number="form.goodPct" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="民主测评较好的比例" prop="littleGoodPct">
            <el-input v-model.number="form.littleGoodPct" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="民主测评一般的比例" prop="generalPct">
            <el-input v-model.number="form.generalPct" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="民主测评较差的比例" prop="littleBadPct">
            <el-input v-model.number="form.littleBadPct" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="考核结论">
            <el-input type="text" v-model="form.evalConclude" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="受表彰情况">
            <el-input type="textarea" :rows="2" v-model="form.citeCase" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="被责任追究情况">
            <el-input type="textarea" :rows="2" v-model="form.dutyChargeCase" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="表彰或追究单位">
            <el-input type="text" v-model="form.processUnit" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="文号">
            <el-input type="text" v-model="form.refNo" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleUpdate">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateUserDuty, createUserDuty } from '@/api/archives'
import { openNotification, getSession, validateNum } from '@/utils/util'

export default {
  data() {
    return {
      isEdit: false,
      visible: false,
      labelPosition: 'right',
      form: {},
      inputWidth: {
        width: '100%'
      },
      rules: {
        goodPct: validateNum,
        littleGoodPct: validateNum,
        generalPct: validateNum,
        littleBadPct: validateNum
      },
      leaderRow: JSON.parse(this.$route.query.leaderRow)
    }
  },
  methods: {
    edit(bool, obj) {
      this.isEdit = bool
      this.visible = true
      this.form = Object.assign({}, obj)
      this.$set(this.form, 'userId', this.leaderRow.userId)
    },
    handleUpdate() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let res = null,
            mes = null
          if (this.isEdit) {
            res = await updateUserDuty(this.form)
            mes = '修改操作成功'
          } else {
            res = await createUserDuty(this.form)
            mes = '新增操作成功'
          }
          if (res.state === '000000') {
            openNotification('success', mes)
            this.visible = false
            this.$emit('refresh')
          }
        }
      })
    }
  }
}
</script>
